/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UploadIndexFrm.java
 *
 * Created on 8/08/2012, 10:39:50 AM
 */

package mx.jmcprograms.uploadindex.ui;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.FileWriter;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.swing.JOptionPane;

import mx.jmcprograms.uploadindex.util.FTPClientMgr;


/**
 *
 * @author Administrador
 */
public class UploadIndexFrm extends javax.swing.JFrame {

    /** Creates new form UploadIndexFrm */
    public UploadIndexFrm() {
        initComponents();
        
        try {
			InetAddress addr = InetAddress.getLocalHost();
			iplocalTxt.setText(addr.toString().split("/")[1]);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
        


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jTabbedPane12 = new javax.swing.JTabbedPane();
        configPanel = new javax.swing.JPanel();
        iplocalTxt = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        serverFtpTxt = new javax.swing.JTextField();
        userFtpTxt = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        passwordFtpTxt = new javax.swing.JPasswordField();
        jLabel5 = new javax.swing.JLabel();
        pathTxt = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        pathFTPTxt = new javax.swing.JTextField();
        uploadBtn = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        iptelmexTxt = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        puertoTxt = new javax.swing.JTextField();
        subirArchivoChk = new javax.swing.JCheckBox();
        generaBtn = new javax.swing.JButton();
        logPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        logTxt = new javax.swing.JTextArea();
        editorPanel = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        editorTxt = new javax.swing.JTextArea();
        jPanel8 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        fileNameTxt = new javax.swing.JTextField();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Upload Index - JMPROGRAMS.COM.MX");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jPanel1.setPreferredSize(new java.awt.Dimension(400, 10));
        jPanel1.setLayout(null);
        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_START);

        jPanel2.setPreferredSize(new java.awt.Dimension(400, 20));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 532, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 20, Short.MAX_VALUE)
        );

        getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_END);

        jPanel4.setLayout(new java.awt.BorderLayout());

        configPanel.setLayout(null);
        configPanel.add(iplocalTxt);
        iplocalTxt.setBounds(50, 110, 140, 20);

        jLabel1.setText("IPLocal");
        configPanel.add(jLabel1);
        jLabel1.setBounds(50, 90, 120, 14);

        jLabel2.setText("Server ftp");
        configPanel.add(jLabel2);
        jLabel2.setBounds(240, 30, 140, 14);

        serverFtpTxt.setText("67.23.237.158");
        configPanel.add(serverFtpTxt);
        serverFtpTxt.setBounds(240, 50, 140, 20);

        userFtpTxt.setText("jmcprogr");
        configPanel.add(userFtpTxt);
        userFtpTxt.setBounds(240, 90, 140, 20);

        jLabel3.setText("Usuario");
        configPanel.add(jLabel3);
        jLabel3.setBounds(240, 70, 140, 14);

        jLabel4.setText("Password");
        configPanel.add(jLabel4);
        jLabel4.setBounds(240, 110, 140, 14);

        passwordFtpTxt.setText("sEicnRGK--");
        configPanel.add(passwordFtpTxt);
        passwordFtpTxt.setBounds(240, 130, 140, 20);

        jLabel5.setText("Ruta de archivo a subir");
        configPanel.add(jLabel5);
        jLabel5.setBounds(50, 250, 150, 14);

        pathTxt.setText("C:\\jmcprograms\\docs\\index.html");
        configPanel.add(pathTxt);
        pathTxt.setBounds(50, 270, 460, 20);

        jButton1.setText("Examinar...");
        configPanel.add(jButton1);
        jButton1.setBounds(240, 300, 160, 23);

        jLabel6.setText("Ruta destino ftp");
        configPanel.add(jLabel6);
        jLabel6.setBounds(50, 160, 170, 14);

        pathFTPTxt.setText("/public_html/espia/index.html");
        configPanel.add(pathFTPTxt);
        pathFTPTxt.setBounds(50, 180, 460, 20);

        uploadBtn.setText("Upload");
        uploadBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                uploadBtnActionPerformed(evt);
            }
        });
        configPanel.add(uploadBtn);
        uploadBtn.setBounds(140, 210, 250, 23);

        jLabel7.setText("IP TELMEX dinamica publica");
        configPanel.add(jLabel7);
        jLabel7.setBounds(50, 10, 180, 14);
        configPanel.add(iptelmexTxt);
        iptelmexTxt.setBounds(50, 30, 140, 20);

        jLabel8.setText("Puerto Tomcat");
        configPanel.add(jLabel8);
        jLabel8.setBounds(50, 50, 140, 14);

        puertoTxt.setText("8080");
        configPanel.add(puertoTxt);
        puertoTxt.setBounds(50, 70, 140, 20);

        subirArchivoChk.setText("subir archivo");
        configPanel.add(subirArchivoChk);
        subirArchivoChk.setBounds(50, 290, 120, 23);

        generaBtn.setText("genera index");
        generaBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generaBtnActionPerformed(evt);
            }
        });
        configPanel.add(generaBtn);
        generaBtn.setBounds(50, 130, 140, 23);

        jTabbedPane12.addTab("CONFIG", configPanel);

        logPanel.setLayout(new java.awt.BorderLayout());

        logTxt.setColumns(20);
        logTxt.setRows(5);
        jScrollPane1.setViewportView(logTxt);

        logPanel.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jTabbedPane12.addTab("LOG", logPanel);

        editorPanel.setLayout(new java.awt.BorderLayout());

        jPanel7.setLayout(new java.awt.BorderLayout());

        editorTxt.setColumns(20);
        editorTxt.setRows(5);
        jScrollPane2.setViewportView(editorTxt);

        jPanel7.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jPanel8.setPreferredSize(new java.awt.Dimension(818, 30));
        jPanel8.setLayout(null);

        jLabel9.setText("Archivo:");
        jPanel8.add(jLabel9);
        jLabel9.setBounds(0, 0, 50, 14);

        fileNameTxt.setText("index.html");
        jPanel8.add(fileNameTxt);
        fileNameTxt.setBounds(50, 0, 460, 20);

        jPanel7.add(jPanel8, java.awt.BorderLayout.PAGE_START);

        editorPanel.add(jPanel7, java.awt.BorderLayout.CENTER);

        jTabbedPane12.addTab("Editor", editorPanel);

        jPanel4.add(jTabbedPane12, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel4, java.awt.BorderLayout.CENTER);

        jMenu1.setText("File");

        jMenuItem1.setText("Salida");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Genera");
        jMenu2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu2ActionPerformed(evt);
            }
        });

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F8, 0));
        jMenuItem2.setText("genera index");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem2);

        jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F9, 0));
        jMenuItem3.setText("upload");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem3);

        jMenuBar1.add(jMenu2);

        jMenu3.setText("Ayuda");

        jMenuItem4.setText("Acerca de ...");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem4);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void uploadBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_uploadBtnActionPerformed
        //SFTPClient ftp=new SFTPClient(this.userFtpTxt.getText(),this.passwordFtpTxt.getText(),this.serverFtpTxt.getText());
    	FTPClientMgr ftp=new FTPClientMgr(this.userFtpTxt.getText(),this.passwordFtpTxt.getText(),this.serverFtpTxt.getText());
    	try {
			ftp.sendFile(pathTxt.getText().trim(), pathFTPTxt.getText().trim());
		} catch (IOException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
		}
		JOptionPane.showMessageDialog(null, "ya se subio el archivo","UploadOK",JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_uploadBtnActionPerformed

    private void generaBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generaBtnActionPerformed
        try {
        	String content="<head>\n<meta http-equiv=\"refresh\" content=\"0;url=http://"+iptelmexTxt.getText()+":"+puertoTxt.getText()+"/bbsightsell/index.jsp\">\n</head>";
        	editorTxt.setText(content);
			FileWriter fw=new FileWriter(pathTxt.getText());
			fw.write(this.editorTxt.getText());
			fw.close();
                JOptionPane.showMessageDialog(null, "Archivo guardado","Guardado",JOptionPane.INFORMATION_MESSAGE);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
		}
    }//GEN-LAST:event_generaBtnActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        System.exit(1);
    }//GEN-LAST:event_formWindowClosing

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        System.exit(1);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        this.generaBtnActionPerformed(evt);
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        this.uploadBtnActionPerformed(evt);
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenu2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu2ActionPerformed
        
    }//GEN-LAST:event_jMenu2ActionPerformed

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        JOptionPane.showMessageDialog(null,"JMC Programs 2012 Juan Miguel Olguin Salguero","Error", JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    	
    	
    	try {
            javax.swing.UIManager.setLookAndFeel(
                    "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"
                    //javax.swing.UIManager.getSystemLookAndFeelClassName()
                    //javax.swing.UIManager.getCrossPlatformLookAndFeelClassName()
                    //"org.jvnet.substance.SubstanceLookAndFeel"
                    );
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
            	UploadIndexFrm form = null;
                form= new UploadIndexFrm();
                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        		int maxX=540;
        		int maxY=454;
        		form.setSize(new Dimension(maxX,maxY));
        		form.setLocation((screenSize.width-maxX)/2, (screenSize.height-maxY)/2);
                form.setVisible(true);
            	form.setResizable(false);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel configPanel;
    private javax.swing.JPanel editorPanel;
    private javax.swing.JTextArea editorTxt;
    private javax.swing.JTextField fileNameTxt;
    private javax.swing.JButton generaBtn;
    private javax.swing.JTextField iplocalTxt;
    private javax.swing.JTextField iptelmexTxt;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane12;
    private javax.swing.JPanel logPanel;
    private javax.swing.JTextArea logTxt;
    private javax.swing.JPasswordField passwordFtpTxt;
    private javax.swing.JTextField pathFTPTxt;
    private javax.swing.JTextField pathTxt;
    private javax.swing.JTextField puertoTxt;
    private javax.swing.JTextField serverFtpTxt;
    private javax.swing.JCheckBox subirArchivoChk;
    private javax.swing.JButton uploadBtn;
    private javax.swing.JTextField userFtpTxt;
    // End of variables declaration//GEN-END:variables

}
